<?php
include 'Config.php';
include 'Mysql_class.php';
include 'Define.php';
include 'Page_class.php';
include_once str_replace( '\\' , '/' , realpath(dirname(__FILE__).'/../')).'/compile/admin/admin_template.php';
$tpl = new Template(array('debug' => true));
power();
//获取系统设置
if(!isset($results)){
	$db = new db($conf);
	$fields = array("*");
	$results = $db->field($fields)->select("sc_system");
	$results = $results[0];
}

if($_SESSION['a']['alevel']==3){
	$db = new db($conf);
	$result = $db->field(array("*"))->where("uid=".$_SESSION['a']['auserid']." and uid<>1")->select("sc_power");
	$power = json_decode($result[0]['power'],true);
}

//成功返回
function success($msg){
	$str = array(1,$msg);
	echo json_encode($str);
	exit;
}
//失败返回
function error($msg){
	$str = array(0,$msg);
	echo json_encode($str);
	exit;
}
//轮播图设置
function lunbo($t){
	global $conf;
	$db = new db($conf);
	$list = $db->field(array("*"))->where("type=$t")->order("Id asc")->select("sc_lunbo");
	return $list;
}
//获取制度信息
function zdset(){
	global $conf;
	$db = new db($conf);
	$info = $db->field(array("*"))->select("sc_set");
	return $info[0];
}

function czlist($page,$psize,$uname,$stime,$etime,$state){
	global $conf;
	$db = new db($conf);
	$where = "c.Id>0";
	if($uname){
		$r = $db->field(array("Id"))->where("uname='".$uname."'")->select("sc_member");
		if($r){
			$where = $where." and c.uid=".$r[0]['Id'];
		}
	}
	if($stime){
		$stime = $stime." 10:12:22";
		$where = $where." and to_days(c.addtime)>=to_days('".$stime."')";
	}
	if($etime){
		$etime = $etime." 10:12:22";
		$where = $where." and to_days(c.addtime)<=to_days('".$etime."')";
	}
	if($state<3){
	    $where = $where." and c.state=$state";
	}
	$list = $db->field(array("c.*","m.uname","m.tname"))->where($where)->order("c.Id desc")->limit($page,$psize)->select("sc_cashcz as c left join sc_member as m on c.uid=m.Id");
	
	return $list;
}

function czlistnum($uname,$stime,$etime,$state){
	global $conf;
	$db = new db($conf);
	$where = "Id>0";
	if($uname){
		$r = $db->field(array("Id"))->where("uname='".$uname."'")->select("sc_member");
		if($r){
			$where = $where." and uid=".$r[0]['Id'];
		}
	}
	if($stime){
		$stime = $stime." 10:12:22";
		$where = $where." and to_days(addtime)>=to_days('".$stime."')";
	}
	if($etime){
		$etime = $etime." 10:12:22";
		$where = $where." and to_days(addtime)<=to_days('".$etime."')";
	}
	if($state<3){
	    $where = $where." and state=$state";
	}
	$result = $db->field(array("count(*)"))->where($where)->select("sc_cashcz");	
	return $result[0]['count(*)'];
}

function agentinfo($id){
	global $conf;
	$db = new db($conf);
	$info = $db->field(array("a.*","m.uname","m.tname"))->where("a.Id=$id and a.state=1")->select("sc_agent as a left join sc_member as m on m.Id=a.uid");
	if($info){
		$pro = $cit = $cou = "";
		$list = $db->field(array("*"))->where("areapid=0")->select("sc_area");
		$pro = "<option value='0'>--请选择--</option>";
		for($i=0;$i<count($list);$i++){
			if($list[$i]['areaid']==$info[0]['pro']){
				$pro = $pro."<option selected value='".$list[$i]['areaid']."'>".$list[$i]['areaname']."</option>";
			}else{
				$pro = $pro."<option value='".$list[$i]['areaid']."'>".$list[$i]['areaname']."</option>";
			}			
		}
		if($info[0]['dllevel']>1){
			$cit = "<option value='0'>--请选择--</option>";
			$list = $db->field(array("*"))->where("areapid=".$info[0]['pro'])->select("sc_area");
			for($i=0;$i<count($list);$i++){
				if($list[$i]['areaid']==$info[0]['cit']){
					$cit = $cit."<option selected value='".$list[$i]['areaid']."'>".$list[$i]['areaname']."</option>";
				}else{
					$cit = $cit."<option value='".$list[$i]['areaid']."'>".$list[$i]['areaname']."</option>";
				}			
			}
		}
		if($info[0]['dllevel']==3){
			$cou = "<option value='0'>--请选择--</option>";
			$list = $db->field(array("*"))->where("areapid=".$info[0]['pro'])->select("sc_area");
			for($i=0;$i<count($list);$i++){
				if($list[$i]['areaid']==$info[0]['cou']){
					$cou = $cou."<option selected value='".$list[$i]['areaid']."'>".$list[$i]['areaname']."</option>";
				}else{
					$cou = $cou."<option value='".$list[$i]['areaid']."'>".$list[$i]['areaname']."</option>";
				}			
			}
		}
		$info[0]['pro'] = $pro;
		$info[0]['cit'] = $cit;
		$info[0]['cou'] = $cou;
		return $info[0];
	}else{
		return false;
	}
}

function agentlist($page,$psize,$uname){
	global $conf;
	$db = new db($conf);
	$uid = 0;
	if($uname){
		$info = $db->field(array("*"))->where("uname ='".$uname."' or tname='".$uname."'")->select("sc_member");
		if($info){
			$uid = $info[0]['Id'];
		}
	}
	if($uid>0){
		$list = $db->field(array("a.*","m.uname","m.tname"))->where("a.state=1 and a.uid=$uid")->order("a.Id desc")->limit($page,$psize)->select("sc_agent as a left join sc_member as m on m.Id=a.uid");
	}else{
		$list = $db->field(array("a.*","m.uname","m.tname"))->where("a.state=1")->order("a.Id desc")->limit($page,$psize)->select("sc_agent as a left join sc_member as m on m.Id=a.uid");
	}
	for($i=0;$i<count($list);$i++){
		$str = $list[$i]['pro'];
		if($list[$i]['cit']>0){
			$str = $str.",".$list[$i]['cit'];
		}
		if($list[$i]['cou']>0){
			$str = $str.",".$list[$i]['cou'];
		}
		$alist = $db->field(array("*"))->where("areaid in($str)")->order("areaid asc")->select("sc_area");
		$area = "";
		for($n=0;$n<count($alist);$n++){
			$area = $area.$alist[$n]['areaname'];
		}
		$list[$i]['area'] = $area;		
	}
	return $list;
}

function agentnum($uname){
	global $conf;
	$db = new db($conf);
	$uid = 0;
	if($uname){
		$info = $db->field(array("*"))->where("uname ='".$uname."' or tname='".$uname."'")->select("sc_member");
		if($info){
			$uid = $info[0]['Id'];
		}
	}
	if($uid>0){
		$list = $db->field(array("count(*)"))->where("state=1 and uid=$uid")->select("sc_agent");
	}else{
		$list = $db->field(array("count(*)"))->where("state=1")->select("sc_agent");
	}
		
	return $list[0]['count(*)'];
}

function agentrecord($page,$psize,$uname){
	global $conf;
	$db = new db($conf);
	$uid = 0;
	if($uname){
		$info = $db->field(array("*"))->where("uname ='".$uname."' or tname='".$uname."'")->select("sc_member");
		if($info){
			$uid = $info[0]['Id'];
		}
	}
	if($uid>0){
		$list = $db->field(array("a.*","m.uname","m.tname"))->where("a.uid=$uid")->order("a.Id desc")->limit($page,$psize)->select("sc_agent_record as a left join sc_member as m on m.Id=a.uid");
	}else{
		$list = $db->field(array("a.*","m.uname","m.tname"))->order("a.Id desc")->limit($page,$psize)->select("sc_agent_record as a left join sc_member as m on m.Id=a.uid");
	}
	
	for($i=0;$i<count($list);$i++){
		$str = $list[$i]['pro'];
		if($list[$i]['cit']>0){
			$str = $str.",".$list[$i]['cit'];
		}
		if($list[$i]['cou']>0){
			$str = $str.",".$list[$i]['cou'];
		}
		$alist = $db->field(array("*"))->where("areaid in($str)")->order("areaid asc")->select("sc_area");
		$area = "";
		for($n=0;$n<count($alist);$n++){
			$area = $area.$alist[$n]['areaname'];
		}
		$list[$i]['area'] = $area;		
	}
	return $list;
}

function getnews($nid){
	global $conf;
	$db = new db($conf);
	$info = $db->field(array("*"))->where("Id=$nid")->select("sc_news");
	if($info){
		$cat = $db->field(array("*"))->where("lx=".$info[0]['lx'])->select("sc_menu");		
		$info[0]['content'] = htmlspecialchars_decode($info[0]['content']);
		$info[0]['pic1'] = json_decode($info[0]['pic1'],true);
		return $info[0];
	}else{
		return false;
	}	
}

function newslist($page=1,$pagesize=10,$cid=1){
	global $conf;
	$db = new db($conf);
	if($cid){
		$list = $db->field(array("n.*","c.name"))->where("n.cid=$cid")->limit($page,$pagesize)->order("n.Id desc")->select("sc_news as n left join sc_menu as c on c.Id=n.cid");
	}else{
		$list = $db->field(array("n.*","c.name"))->limit($page,$pagesize)->order("n.Id desc")->select("sc_news as n left join sc_menu as c on c.Id=n.cid");
	}
	for($i=0;$i<count($list);$i++){
		$list[$i]['pic1'] = json_decode($list[$i]['pic1'],true);
	}
	return $list;
}

function newslistnum($cid=1){
	global $conf;
	$db = new db($conf);
	if($cid){
		$result = $db->field(array("count(*)"))->where("cid=$cid")->select("sc_news");
	}else{
		$result = $db->field(array("count(*)"))->select("sc_news");
	}
	
	return $result[0]['count(*)'];
}

function menuid($id){
	global $conf;
	$db = new db($conf);
	$list = $db->field(array("*"))->where("Id=$id")->select("sc_menu");
	if($list){
		return $list[0];
	}else{
		return false;
	}
}

function menulist(){
	global $conf;
	$db = new db($conf);
	$list = $db->field(array("*"))->select("sc_menu");
	return $list;
}

function agentrecordnum($uname){
	global $conf;
	$db = new db($conf);
	$uid = 0;
	if($uname){
		$info = $db->field(array("*"))->where("uname ='".$uname."' or tname='".$uname."'")->select("sc_member");
		if($info){
			$uid = $info[0]['Id'];
		}
	}
	if($uid>0){
		$list = $db->field(array("count(*)"))->where("uid=$uid")->select("sc_agent_record");	
	}else{
		$list = $db->field(array("count(*)"))->select("sc_agent_record");	
	}
	
	return $list[0]['count(*)'];
}


function prolist(){
	global $conf;
	$db = new db($conf);
	$list = $db->field(array("*"))->where("areapid=0")->select("sc_area");
	$str = "<option value='0'>--请选择--</option>";
	for($i=0;$i<count($list);$i++){
		$str = $str."<option value='".$list[$i]['areaid']."'>".$list[$i]['areaname']."</option>";
	}
	return $str;
}

function tjlist($page,$psize,$stime,$etime){
    global $conf;
	$db = new db($conf);
	$where = "Id>0";
	if($stime){
		$where .= " and to_days(addtime)>=to_days('".$stime."')";
	}
	if($etime){
		$where .= " and to_days(addtime)<=to_days('".$etime."')";
	}
	$list = $db->field(array("*"))->where($where)->order("Id desc")->limit($page,$psize)->select("sc_yeji");
	for($i=0;$i<count($list);$i++){
	    $list[$i]['addtime'] = date("Y-m-d",strtotime($list[$i]['addtime']));
		$list[$i]['allcash'] = $list[$i]['cash1'] + $list[$i]['cash2'] + $list[$i]['cash3'];
	}
	return $list;
}

function tjlistnum($stime,$etime){
    global $conf;
	$db = new db($conf);
	$where = "Id>0";
	if($stime){
		$where .= " and to_days(addtime)>=to_days('".$stime."')";
	}
	if($etime){
		$where .= " and to_days(addtime)<=to_days('".$etime."')";
	}
	$list = $db->field(array("count(*)"))->where($where)->select("sc_yeji");
	return $list[0]['count(*)'];
}

/* 
function tjinfo($stime,$etime){
    global $conf;
	$db = new db($conf);
	$where = "Id>0";
	if($stime){
		$where .= " and to_days(addtime)>=to_days('".$stime."')";
	}
	if($etime){
		$where .= " and to_days(addtime)<=to_days('".$etime."')";
	}
	$list = $db->field(array("SUM(cash1) as bdcash","SUM(cash2) as sccash","SUM(yue1) as yue1","SUM(yue2) as yue2","SUM(weixin1) as weixin1","SUM(weixin2) as weixin2","SUM(zhifubao1) as zhifubao1","SUM(zhifubao2) as zhifubao2"))->where($where)->select("sc_yeji");
	$info['all'] = $list[0]['bdcash'] + $list[0]['sccash'];
	$info['ye'] = $list[0]['yue1'] + $list[0]['yue2'];
	$info['wx'] = $list[0]['weixin1'] + $list[0]['weixin2'];
	$info['zfb'] = $list[0]['zhifubao1'] + $list[0]['zhifubao2'];
	return $info;
} */

function admininfo(){
	global $conf;
	$db = new db($conf);
	$info = $db->field(array("*"))->where("Id=".$_SESSION['a']['auserid'])->select("sc_admin");
	return $info[0];
}

function userinfo1($id){
	global $conf;
	$db = new db($conf);
	$result = $db->field(array("*"))->where(array("Id"=>$id))->select("sc_member");	
	return $result[0];
}

function orderlist($page,$psize,$uid,$state,$code,$tname,$uname,$time,$gname,$cplx,$type,$ocode,$num,$oid,$hjcode){
	global $conf;
	$db = new db($conf);
	$where = " o.state<5 and  o.state>0 ";
	if($uid){
		$where = $where." and o.uid=$uid ";
	}
	if($state<5){
		$where = $where." and o.state=$state";
	}
	if($code){
		$where = $where." and o.ztcode='".$code."'";
	}
	if($hjcode){
		$where = $where." and o.trackcode1='".$hjcode."'";
	}
	if($tname){
		$where = $where." and o.tname='".$tname."'";
	}
	if($uname){
		$info = $db->field(array("Id"))->where("uname='".$uname."'")->select("sc_member");
		if($info){
			$where = $where." and o.uid=".$info[0]['Id'];
		}else{
			$where = $where." and o.uid=0";
		}	
	}
	if($time){
		$time = $time." 00:00:00";
		$where = $where." and to_days(o.addtime)= to_days('".$time."')";
	}
	if($gname){
		$where = $where." and o.content like '%".$gname."%'";
	}
	if($cplx){
		$where = $where." and o.cplx='".$cplx."'";
	}
	if($type){
		$where = $where." and o.type='".$type."'";
	}
	if($ocode){
		$where = $where." and o.ocode='".$ocode."'";
	}
	if($oid>0){
		$where .= " and o.Id=$oid";
	}
	$list = $db->field(array("o.*","m.uname","m.tname as mtname","m.phone as mphone"))->where($where)->order("o.Id desc")->limit($page,$psize)->select("sc_orders as o left join sc_member as m on m.Id=o.uid");	
	for($i=0;$i<count($list);$i++){
		if(!$list[$i]['tname']){
			$list[$i]['tname'] = $list[$i]['mtname'];
			$list[$i]['phone'] = $list[$i]['mphone'];
		}
		$list[$i]['hxname'] = '';
		if($list[$i]['hxid']>0){
			$info = $db->field(['*'])->where("Id=".$list[$i]['hxid'])->select("sc_member");
			if($info){
				$list[$i]['hxname'] = $list[$i]['uname'].'['.$list[$i]['tname'].']';
			}
		}
		$list[$i]['px'] = $num - (($page - 1)*$psize + $i);
	}
	return $list;
}

function orderlistnum($uid,$state,$code,$tname,$uname,$time,$gname,$cplx,$type,$ocode,$oid,$hjcode){
	global $conf;
	$db = new db($conf);
	$where = " state<5 and state>0 ";
	if($uid){
		$where = $where." and uid=$uid ";
	}
	if($state<5){
		$where = $where." and state=$state";
	}
	if($code){
		$where = $where." and ztcode='".$code."'";
	}
	if($hjcode){
		$where = $where." and trackcode1='".$hjcode."'";
	}
	if($tname){
		$where = $where." and tname='".$tname."'";
	}
	if($uname){
		$info = $db->field(array("Id"))->where("uname='".$uname."'")->select("sc_member");
		if($info){
			$where = $where." and uid=".$info[0]['Id'];
		}else{
			$where = $where." and uid=0";
		}	
	}
	if($time){
		$time = $time." 00:00:00";
		$where = $where." and to_days(addtime)= to_days('".$time."')";
	}
	if($gname){
		$where = $where." and content like '%".$gname."%'";
	}
	if($cplx){
		$where = $where." and cplx='".$cplx."'";
	}
	if($type){
		$where = $where." and type='".$type."'";
	}
	if($ocode){
		$where = $where." and ocode='".$ocode."'";
	}
	if($oid>0){
		$where .= " and Id=$oid";
	}
	$result = $db->field(array("count(*)"))->where($where)->select("sc_orders");
	
	return $result[0]['count(*)'];
}

function orders($id){
	global $conf;
	$db = new db($conf);
	$result = $db->field(array("o.*","m.uname"))->where(array("o.Id"=>$id))->select("sc_orders as o left join sc_member as m on m.Id=o.uid");	
	return $result[0];
}

function infolist($page=1,$pagesize=10){
	global $conf;
	$db = new db($conf);
	$result = $db->field(array("*"))->limit($page,$pagesize)->order("Id desc")->select("sc_info");
	return $result;
}

function infolistnum(){
	global $conf;
	$db = new db($conf);
	$result = $db->field(array("count(*)"))->select("sc_info");
	return $result[0]['count(*)'];
}

function getinfo($id){
	global $conf;
	$db = new db($conf);
	$result = $db->field(array("*"))->where(array("Id"=>$id))->select("sc_info");
	$info = $result[0];
	return $info;
}

function alltxcash($uname,$time){
	global $conf;
	$db = new db($conf);
	$where = "state<2 ";	
	if($uname){
		$info = $db->field(array("Id"))->where("uname='".$uname."'")->select("sc_member");
		if($info){
			$where = $where." and uid=".$info[0]['Id'];
		}else{
			$where = $where." and uid=0";
		}	
	}
	if($time){
		$time = $time." 00:00:00";
		$where = $where." and to_days(addtime) = to_days('".$time."')";
	}
	$info = $db->field(array("SUM(cash)","SUM(num)"))->where($where)->select("sc_tixian");
	if(!$info[0]['SUM(cash)']){
		$info[0]['SUM(cash)'] = 0;
	}else{
		$info[0]['SUM(cash)'] = round($info[0]['SUM(cash)'],2);
	}
	if(!$info[0]['SUM(num)']){
		$info[0]['SUM(num)'] = 0;
	}else{
		$info[0]['SUM(num)'] = round($info[0]['SUM(num)'],2);		
	}
	return $info[0];
}

function yecash(){
	global $conf;
	$db = new db($conf);
	$info = $db->field(array("SUM(cash)","SUM(jjcash)"))->select("sc_member");
	if($info[0]['SUM(cash)']){
		$str['cash'] = round($info[0]['SUM(cash)'],2);
	}else{
		$str['cash'] = 0;
	}
	if($info[0]['SUM(jjcash)']){
		$str['jjcash'] = round($info[0]['SUM(jjcash)'],2);
	}else{
		$str['jjcash'] = 0;
	}	
	return $str;
}

function allczcash($uid,$uname,$stime,$etime,$type){
	global $conf;
	$db = new db($conf);
	$where = "Id>0 ";
	if($uid){
		$where = $where." and uid = $uid ";
	}
	if($uname){
		$info = $db->field(array("Id"))->where("uname='".$uname."'")->select("sc_member");
		if($info){
			$where = $where." and uid=".$info[0]['Id'];
		}else{
			$where = $where." and uid=0";
		}	
	}
	if($stime){
		$stime = $stime." 00:00:00";
		$where = $where." and to_days(addtime) >= to_days('".$stime."')";
	}
	if($etime){
		$etime = $etime." 00:00:00";
		$where = $where." and to_days(addtime) <= to_days('".$etime."')";
	}
	if($type>0){
		$where = $where." and type=$type";
	}
	$info = $db->field(array("SUM(cash)"))->where($where)->select("sc_cz");
	if($info[0]['SUM(cash)']){
		return $info[0]['SUM(cash)'];
	}else{
		return 0;
	}
}

function cashlistcz($page=1,$psize=20,$uid,$uname,$stime,$etime,$type){
	global $conf;
	$db = new db($conf);
	$where = "c.uid = m.Id ";
	if($uid){
		$where = $where."and c.uid = $uid ";
	}
	if($uname){
		$info = $db->field(array("Id"))->where("uname='".$uname."'")->select("sc_member");
		if($info){
			$where = $where." and c.uid=".$info[0]['Id'];
		}else{
			$where = $where." and c.uid=0";
		}	
	}
	if($stime){
		$stime = $stime." 00:00:00";
		$where = $where." and to_days(c.addtime) >= to_days('".$stime."')";
	}
	if($etime){
		$etime = $etime." 00:00:00";
		$where = $where." and to_days(c.addtime) <= to_days('".$etime."')";
	}
	if($type>0){
		$where = $where." and c.type=$type";
	}
	$result = $db->field(array("c.*","m.uname","m.tname"))->where($where)->order("c.Id desc")->limit($page,$psize)->select("sc_cz as c,sc_member as m");
	if($result){
		return $result;
	}
	else{
		return false;
	}
}

function cashlistnumcz($uid,$uname,$stime,$etime,$type){
	global $conf;
	$db = new db($conf);
	$where = "c.uid = m.Id ";
	if($uid){
		$where = $where."and c.uid = $uid ";
	}
	if($uname){
		$info = $db->field(array("Id"))->where("uname='".$uname."'")->select("sc_member");
		if($info){
			$where = $where." and c.uid=".$info[0]['Id'];
		}else{
			$where = $where." and c.uid=0";
		}	
	}
	if($stime){
		$stime = $stime." 00:00:00";
		$where = $where." and to_days(c.addtime) >= to_days('".$stime."')";
	}
	if($etime){
		$etime = $etime." 00:00:00";
		$where = $where." and to_days(c.addtime) <= to_days('".$etime."')";
	}
	if($type>0){
		$where = $where." and c.type=$type";
	}
	$result = $db->field(array("count(*)"))->where($where)->select("sc_cz as c,sc_member as m");
	return $result[0]['count(*)'];
}

function hzlist($page=1,$psize=10,$uid,$rid){
	global $conf;
	$db = new db($conf);
	$where = "h.state > 0 and h.type=1 ";
	if($uid){
		$where = $where." and h.uid=$uid";
	}
	if($rid){
		$where = $where." and h.rid=$rid";
	}
	$result = $db->field(array("h.*","m.uname","m.tname"))->where($where)->order("h.Id desc")->limit($page,$psize)->select("sc_huzhuan as h left join sc_member as m on m.Id=h.uid");
	for($i=0;$i<count($result);$i++){
		$r1 = $db->field(array("uname","tname"))->where("Id=".$result[$i]['rid'])->select("sc_member");
		if($r1){
			$result[$i]['runame'] = $r1[0]['uname'];
			$result[$i]['rtname'] = $r1[0]['tname'];
		}else{
			$result[$i]['runame'] = "";
			$result[$i]['rtname'] = "";
		}
	}
	return $result;
}

function hzlistnum($uid,$rid){
	global $conf;
	$db = new db($conf);
	$where = "state > 0 and type=1 ";
	if($uid){
		$where = $where." and uid=$uid";
	}
	if($rid){
		$where = $where." and rid=$rid";
	}
	$result = $db->field(array("count(*)"))->where($where)->select("sc_huzhuan");

	return $result[0]['count(*)'];
}

function record_ye_list($page,$psize,$uname,$stime,$etime){
	global $conf;
	$db = new db($conf);
	$where = "c.type>0";
	if($uname){
		$r = $db->field(array("Id"))->where("uname='".$uname."'")->select("sc_member");
		if($r){
			$where = $where." and c.uid=".$r[0]['Id'];
		}
	}
	if($stime){
		$stime = $stime." 10:12:22";
		$where = $where." and to_days(c.addtime)>=to_days('".$stime."')";
	}
	if($etime){
		$etime = $etime." 10:12:22";
		$where = $where." and to_days(c.addtime)<=to_days('".$etime."')";
	}
	
	$list = $db->field(array("c.*","m.uname","m.tname"))->where($where)->order("c.Id desc")->limit($page,$psize)->select("sc_spend as c left join sc_member as m on c.uid=m.Id");	
	return $list;
}

function record_ye_list_num($uname,$stime,$etime){
	global $conf;
	$db = new db($conf);
	$where = "type>0";
	if($uname){
		$r = $db->field(array("Id"))->where("uname='".$uname."'")->select("sc_member");
		if($r){
			$where = $where." and uid=".$r[0]['Id'];
		}
	}
	if($stime){
		$stime = $stime." 10:12:22";
		$where = $where." and to_days(addtime)>=to_days('".$stime."')";
	}
	if($etime){
		$etime = $etime." 10:12:22";
		$where = $where." and to_days(addtime)<=to_days('".$etime."')";
	}
	$result = $db->field(array("count(*)"))->where($where)->select("sc_spend");	
	return $result[0]['count(*)'];
}

function txrecordlist($page=1,$pagesize=10,$uname,$stime,$etime,$type){
	global $conf;
	$db = new db($conf);
	$where = "t.state<4";
	if($uname){
		$info = $db->field(array("Id"))->where("uname='".$uname."'")->select("sc_member");
		if($info){
			$where = $where." and t.uid=".$info[0]['Id'];
		}
	}
	if($stime){
		$stime = $stime." 00:00:00";
		$where = $where." and to_days(t.addtime)>= to_days('".$stime."')";
	}
	if($etime){
		$etime = $etime." 00:00:00";
		$where = $where." and to_days(t.addtime)<= to_days('".$etime."')";
	}
	if($type>0){
		$where .= " and t.type=$type";
	}
	$result = $db->field(array("t.*","m.tname","m.uname"))->where($where)->order("t.Id desc")->limit($page,$pagesize)->select("sc_tixian as t left join sc_member as m on m.Id=t.uid");
	
	return $result;
}

function txrecordnum($uname,$stime,$etime,$type){
	global $conf;
	$db = new db($conf);
	$where = "state<4";
	if($uname){
		$info = $db->field(array("Id"))->where("uname='".$uname."'")->select("sc_member");
		if($info){
			$where = $where." and uid=".$info[0]['Id'];
		}
	}
	if($stime){
		$stime = $stime." 00:00:00";
		$where = $where." and to_days(addtime)>= to_days('".$stime."')";
	}
	if($etime){
		$etime = $etime." 00:00:00";
		$where = $where." and to_days(addtime)<= to_days('".$etime."')";
	}
	if($type>0){
		$where .= " and type=$type";
	}
	$result = $db->field(array("count(*)"))->where($where)->select("sc_tixian");	
	return $result[0]['count(*)'];
}

function jjinfo($uname,$stime,$etime){
	global $conf;
	$db = new db($conf);
	$where = "adda=1";
	if($uname){
		$r = $db->field(array("Id"))->where("uname='".$uname."'")->select("sc_member");
		if($r){
			$where = $where." and uid=".$r[0]['Id'];
		}
	}
	if($stime){
		$stime = $stime." 10:12:22";
		$where = $where." and to_days(addtime)>=to_days('".$stime."')";
	}
	if($etime){
		$etime = $etime." 10:12:22";
		$where = $where." and to_days(addtime)<=to_days('".$etime."')";
	}
	$list = $db->field(array("SUM(cash)"))->where($where)->select("sc_cash");	
	return $list[0]['SUM(cash)'];
}


function fhlist($page,$psize,$stime,$etime){
	global $conf;
	$db = new db($conf);
	$where = "Id>0";	
	if($stime){
		$stime = $stime." 10:12:22";
		$where = $where." and to_days(addtime)>=to_days('".$stime."')";
	}
	if($etime){
		$etime = $etime." 10:12:22";
		$where = $where." and to_days(addtime)<=to_days('".$etime."')";
	}	
	$list = $db->field(array("*"))->where($where)->order("Id desc")->limit($page,$psize)->select("sc_fenhong");
	return $list;	
}

function fhlistnum($stime,$etime){
	global $conf;
	$db = new db($conf);
	$where = "Id>0";	
	if($stime){
		$stime = $stime." 10:12:22";
		$where = $where." and to_days(addtime)>=to_days('".$stime."')";
	}
	if($etime){
		$etime = $etime." 10:12:22";
		$where = $where." and to_days(addtime)<=to_days('".$etime."')";
	}
	$result = $db->field(array("count(*)"))->where($where)->select("sc_fenhong");		
	return $result[0]['count(*)'];
}


function getdayyeji($time){
	global $conf;
	$db = new db($conf);	
	$list = $db->field(array("*"))->where("to_days(addtime) = to_days('".$time."')")->select("sc_yeji");
	return $list?$list[0]['yjcash']:0;
}

function cashlist($page,$psize,$uname,$stime,$etime){
	global $conf;
	$db = new db($conf);
	$where = "c.type>0";
	if($uname){
		$r = $db->field(array("Id"))->where("uname='".$uname."'")->select("sc_member");
		if($r){
			$where = $where." and c.uid=".$r[0]['Id'];
		}
	}
	if($stime){
		$stime = $stime." 10:12:22";
		$where = $where." and to_days(c.addtime)>=to_days('".$stime."')";
	}
	if($etime){
		$etime = $etime." 10:12:22";
		$where = $where." and to_days(c.addtime)<=to_days('".$etime."')";
	}	
	$list = $db->field(array("c.*","m.uname","m.tname"))->where($where)->order("Id desc")->limit($page,$psize)->select("sc_cash as c left join sc_member as m on c.uid=m.Id");
	return $list;	
}

function cashlistnum($uname,$stime,$etime){
	global $conf;
	$db = new db($conf);
	$where = "type>0";
	if($uname){
		$r = $db->field(array("Id"))->where("uname='".$uname."'")->select("sc_member");
		if($r){
			$where = $where." and uid=".$r[0]['Id'];
		}
	}
	if($stime){
		$stime = $stime." 10:12:22";
		$where = $where." and to_days(addtime)>=to_days('".$stime."')";
	}
	if($etime){
		$etime = $etime." 10:12:22";
		$where = $where." and to_days(addtime)<=to_days('".$etime."')";
	}	
	$result = $db->field(array("count(*)"))->where($where)->select("sc_cash");		
	return $result[0]['count(*)'];
}


//升级记录
function uplist($page,$psize,$u){
	global $conf;
	$db = new db($conf);
	if($u){
		$r1 = $db->field(array("Id"))->where("Id=$u")->select("sc_member");
		if(!$r1){ return false;}
		$list = $db->field(array("u.*","m.tname","m.uname"))->where("uid=".$r1[0]['Id'])->order("u.Id desc")->limit($page,$psize)->select("sc_uplevel as u left join sc_member as m on m.Id=u.uid");
	}else{
		$list = $db->field(array("u.*","m.tname","m.uname"))->order("u.Id desc")->limit($page,$psize)->select("sc_uplevel as u left join sc_member as m on m.Id=u.uid");
	}
	return $list;
}

//升级记录总数
function uplistnum($u){
	global $conf;
	$db = new db($conf);
	if($u){
		$r1 = $db->field(array("Id"))->where("Id=$u")->select("sc_member");
		if(!$r1){ return false;}
		$list = $db->field(array("count(*)"))->where("uid=".$r1[0]['Id'])->select("sc_uplevel");
	}else{
		$list = $db->field(array("count(*)"))->select("sc_uplevel");
	}
	return $list[0]['count(*)'];
}

//留言列表
function messagelist($page=1,$pagesize=10){
	global $conf;
	$db = new db($conf);
	$result = $db->field(array("m.*","u.uname","u.tname"))->limit($page,$pagesize)->order("m.Id desc")->select("sc_message as m left join sc_member as u on u.Id=m.uid");
	for($i=0;$i<count($result);$i++){
		$result[$i]['content'] = strip_tags($result[$i]['content']);
	}
	return $result;
}

//留言列表数量
function messagenum(){
	global $conf;
	$db = new db($conf);
	$result = $db->field(array("count(*)"))->select("sc_message");
	return $result[0]['count(*)'];
}

//留言详情
function message($mid){
	global $conf;
	$db = new db($conf);
	$result = $db->field(array("m.*","u.uname","u.tname"))->where(array("m.Id"=>$mid))->select("sc_message as m left join sc_member as u on u.Id=m.uid");
	if($result){
		return $result[0];
	}else{
		return false;
	}	
}

//单个会员信息
function userinfo($id){
	global $conf;
	$db = new db($conf);
	$result = $db->field(array("*"))->where(array("Id"=>$id))->select("sc_member");
	return $result[0];
}

//商品编辑
function catelist2($id){
	global $conf;
	$db = new db($conf);
	$r1 = $db->field(array("*"))->where("Id=$id")->select("sc_goods");
	if(!$r1){ return false;}
	$r2 = $db->field(array("*"))->where("Id=".$r1[0]['cid'])->select("sc_catogry");
	if(!$r2){
		$r3 = $db->field(array("*"))->where("pid=0")->order("px desc")->select("sc_catogry");
		$str = "<select name='cat' class='cat'>";
		$str = $str."<option value='0'>未选择</option>";
		for($i=0;$i<count($r3);$i++){
			$str = $str."<option value=".$r3[0]['Id'].">".$r3[0]['name']."</option>";
		}
		$str = $str."</select>";
		return $str;
	}
	if($r2[0]['level']==1){
		$r3 = $db->field(array("*"))->where("pid=0")->order("px desc")->select("sc_catogry");
		$str = "<select name='cat' class='cat'>";
		$str = $str."<option value='0'>未选择</option>";
		for($i=0;$i<count($r3);$i++){
			if($r2[0]['Id']==$r3[$i]['Id']){
				$str = $str."<option value=".$r3[$i]['Id']." selected data-id=".$r3[$i]['leixing'].">".$r3[$i]['name']."</option>";
			}else{
				$str = $str."<option value=".$r3[$i]['Id']." data-id=".$r3[$i]['leixing'].">".$r3[$i]['name']."</option>";
			}
		}
		$str = $str."</select>";
		return $str;
	}
	if($r2[0]['level']==2){
		$r3 = $db->field(array("*"))->where("pid=0")->order("px desc")->select("sc_catogry");
		$str = "<select name='cat' class='cat'>";
		$str = $str."<option value='0'>未选择</option>";
		for($i=0;$i<count($r3);$i++){
			if($r2[0]['pid']==$r3[$i]['Id']){
				$str = $str."<option value=".$r3[$i]['Id']." selected  data-id=".$r3[$i]['leixing'].">".$r3[$i]['name']."</option>";
			}else{
				$str = $str."<option value=".$r3[$i]['Id']." data-id=".$r3[$i]['leixing'].">".$r3[$i]['name']."</option>";
			}
		}
		$str = $str."</select><select name='cat1' class='cat1'>";
		$r4 = $db->field(array("*"))->where("pid=".$r2[0]['pid'])->order("px desc")->select("sc_catogry");
		$str = $str."<option value='0'>未选择</option>";
		for($i=0;$i<count($r4);$i++){
			if($r2[0]['Id']==$r4[$i]['Id']){
				$str = $str."<option value=".$r4[$i]['Id']." selected >".$r4[$i]['name']."</option>";
			}else{
				$str = $str."<option value=".$r4[$i]['Id'].">".$r4[$i]['name']."</option>";
			}
		}
		$str = $str."</select>";
		return $str;
	}
}

//单个商品信息展示
function getginfo($id){
	global $conf;
	$db = new db($conf);
	$result = $db->field(array("*"))->where("Id = $id")->select("sc_goods");
	if($result){
		if($result[0]['gthumb1']){
			$result[0]['gthumb1'] = json_decode($result[0]['gthumb1'],true);
		}		
		return $result[0];
	}else{
		"<script>alert('获取商品信息异常，请重试');window.location.href='index.php';</script>";exit;
	}
}

//商品列表页
function catelist1(){
	global $conf;
	$db = new db($conf);
	$result = $db->field(array("*"))->order("px desc")->select("sc_catogry");
	return $result;
}

//二维数组查询
function is_in_array($value,$array){
	if(is_array($array)){
		foreach($array as $item){
			if(is_array($item)){
				if($value == $item['Id']){ return $item["name"];}
				//else{ return false;}
			}
		}
	}
	return false;
	//if()
}


function page1($num,$psize,$page,$url=''){
    global $conf;
    $db = new db($conf);
    if($url){        
        $pagelist = new page($num,$psize,$page,$url."&page={page}",2);
    }else{
        $pagelist = new page($num,$psize,$page,$_SERVER['PHP_SELF']."?page={page}",2);
    }  
    return $pagelist->myde_write();
}

function taglist($type=0,$state=0){
	global $conf;
	$db = new db($conf);
	$where = "level=$type";
	if($state==1){
		$where .= " and state=1";
	}
	$list = $db->field(['*'])->where($where)->select("sc_tag");
	return $list;
}

function goodslist($page,$psize,$cplx,$cid,$tag1,$tag2,$type1,$type2,$type3,$gname){
	global $conf;
	$db = new db($conf);
	$where = 'Id>0';
	$order = '';
	if($cplx>0){
		$where .= " and cplx=$cplx";
	}
	if($cid>0){
		$where .= " and cid=$cid";
	}
	if($tag1>0){
		$where .= " and tag1=$tag1";
	}
	if($tag2>0){
		$where .= " and tag2=$tag2";
	}
	if($type1>0){
		if($type1==1){
			$order = 'cid asc';
		}else{
			$order = 'cid desc';
		}
	}
	if($gname){
		$where .= " and gname like '%".$gname."%'";
	}
	if($type2>0){
		if($order){
			if($type2==1){
				$order .= ',gtotal asc';
			}else{
				$order .= ',gtotal desc';
			}
		}else{
			if($type2==1){
				$order .= 'gtotal asc';
			}else{
				$order .= 'gtotal desc';
			}
		}
	}
	if($type3>0){
		if($order){
			if($type3==1){
				$order .= ',selltotal asc';
			}else{
				$order .= ',selltotal desc';
			}
		}else{
			if($type3==1){
				$order .= 'selltotal asc';
			}else{
				$order .= 'selltotal desc';
			}
		}
	}
	if(!$order){
		$order = 'Id desc';
	}
	$list = $db->field(array("*"))->where($where)->limit($page,$psize)->order($order)->select("sc_goods");
	for($i=0;$i<count($list);$i++){
		$list[$i]['name'] = '';
		$list[$i]['tagname1'] = '';
		$list[$i]['tagname2'] = '';
		$info = $db->field(array("name"))->where("Id=".$list[$i]['cid'])->select("sc_catogry");
		if($info){			
			$list[$i]['name'] = $info[0]['name'];							
		}
		if($list[$i]['tag1']>0){
			$info = $db->field(array("name"))->where("Id=".$list[$i]['tag1'])->select("sc_tag");
			if($info){			
				$list[$i]['tagname1'] = $info[0]['name'];							
			}
		}
		if($list[$i]['tag2']>0){
			$info = $db->field(array("name"))->where("Id=".$list[$i]['tag2'])->select("sc_tag");
			if($info){			
				$list[$i]['tagname2'] = $info[0]['name'];							
			}
		}
	}
	return $list;
}

function goodsnum($cplx,$cid,$tag1,$tag2,$type1,$type2,$type3,$gname){
	global $conf;
	$db = new db($conf);
	$where = 'Id>0';
	if($cplx>0){
		$where .= " and cplx=$cplx";
	}
	if($cid>0){
		$where .= " and cid=$cid";
	}
	if($tag1>0){
		$where .= " and tag1=$tag1";
	}
	if($tag2>0){
		$where .= " and tag2=$tag2";
	}
	if($gname){
		$where .= " and gname like '%".$gname."%'";
	}
	$list = $db->field(array("count(*)"))->where($where)->select("sc_goods");	
	return $list[0]['count(*)'];
}

function catogrylist(){
	global $conf;
	$db = new db($conf);
	$result = $db->field(array("*"))->where("pid=0")->select("sc_catogry");
	$str = "";
	if($result){
		$str = $str."<option value='0'>商品分类</option>";
		for($i=0;$i<count($result);$i++){			
			$str = $str."<option value='".$result[$i]['Id']."' data-id=".$result[$i]['leixing'].">".$result[$i]['name']."</option>";
		}
	}
	else{
		$str = "<option value='0'>请先添加分类</option>";
	}
	return $str;
}
function taginfo($id){
	if($id==0)return false;
	global $conf;
	$db = new db($conf);
	$result = $db->field(array("*"))->where(array("Id"=>$id))->select("sc_tag");
	if($result){
		return $result[0];
	}
	else{
		return false;
	}
}
function cateid($id){
	if($id==0)return false;
	global $conf;
	$db = new db($conf);
	$result = $db->field(array("*"))->where(array("Id"=>$id))->select("sc_catogry");
	if($result){
		return $result[0];
	}
	else{
		return false;
	}
}

function catelist($leixing=0){
	global $conf;
	$db = new db($conf);
	$result = $db->field(array("*"))->where("leixing=$leixing")->order("px desc")->select("sc_catogry");
	return $result;
}

function userlist($page=1,$pagesize=10,$uname,$level,$tlevel){
	global $conf;
	$db = new db($conf);
	$where = "state >= 0 ";
	if($uname){
		$where = $where." and (uname='".$uname."' or tname='".$uname."' or phone='".$uname."')";
	}
	if($level>-1){		
		$where = $where." and level=$level";		
	}
	if($tlevel>0){		
		$where = $where." and tlevel=$tlevel";		
	}
	$result = $db->field(array("*"))->where($where)->order("Id desc")->limit($page,$pagesize)->select("sc_member");	
	for($i=0;$i<count($result);$i++){
		$result[$i]['tjname'] = "";		
		if($result[$i]['tjid']>0){
			$info = $db->field(array("*"))->where("Id=".$result[$i]['tjid'])->select("sc_member");
			if($info){
				$result[$i]['tjname'] = $info[0]['uname']."[".$info[0]['tname']."]";
			}
		}
		$result[$i]['lbname'] = "";		
		if($result[$i]['lbid']>0){
			$info = $db->field(array("*"))->where("Id=".$result[$i]['lbid'])->select("sc_member");
			if($info){
				$result[$i]['lbname'] = $info[0]['uname']."[".$info[0]['tname']."]";
			}
		}
		
		$ztnum = $db->field(['count(*)'])->where('tjid='.$result[$i]['Id'])->select("sc_member");
		$result[$i]['ztnum'] = $ztnum[0]['count(*)']>0?$ztnum[0]['count(*)']:0;
	}
	return $result;
}

function userlistnum($uname,$level,$tlevel){
	global $conf;
	$db = new db($conf);
	$where = "state>=0 ";
	if($uname){
		$where = $where." and (uname='".$uname."' or tname='".$uname."')";
	}
	if($level>-1){		
		$where = $where." and level=$level";		
	}
	if($tlevel>0){		
		$where = $where." and tlevel=$tlevel";		
	}
	$result = $db->field(array("count(*)"))->where($where)->select("sc_member");
	return $result[0]['count(*)'];
}

function usernum($type){
	global $conf;
	$db = new db($conf);
	if($type==1){
		$num = $db->field(['count(*)'])->where("level>0")->select("sc_member");
	}elseif($type==2){
		$num = $db->field(['count(*)'])->where("to_days(addtime) = to_days(now())")->select("sc_member");
	}elseif($type==3){
		$num = $db->field(['count(*)'])->where("to_days(addtime) = to_days(now()) and type=1 and levelb=1")->select("sc_uplevel");
	}else{
		$num = $db->field(['count(*)'])->select("sc_member");
	}
	return $num[0]['count(*)']>0?$num[0]['count(*)']:0;
}

function tjinfo(){
	global $conf;
	$db = new db($conf);
	$info = $db->field(['SUM(cash1)','SUM(cash2)'])->select("sc_yeji");
	$str['cash1'] = $info[0]['SUM(cash1)']>0?round($info[0]['SUM(cash1)'],2):0;
	$str['cash2'] = $info[0]['SUM(cash2)']>0?round($info[0]['SUM(cash2)'],2):0;
	$info = $db->field(['SUM(jjcash1)','SUM(jjcash)'])->select("sc_member");
	$str['jjcash1'] = $info[0]['SUM(jjcash1)']>0?round($info[0]['SUM(jjcash1)'],2):0;
	$str['jjcash'] = $info[0]['SUM(jjcash)']>0?round($info[0]['SUM(jjcash)'],2):0;
	return $str;
}

function powerinfo($id){
	global $conf;
	$db = new db($conf);
	$result = $db->field(array("*"))->where("uid=$id and uid<>1")->select("sc_power");
	$arr = json_decode($result[0]['power'],true);
	return $arr;
}

function managerinfo($id){
	global $conf;
	$db = new db($conf);
	$result = $db->field(array("*"))->where("Id=$id and Id<>1")->select("sc_admin");
	return $result;
}

function managerlist(){
	global $conf;
	$db = new db($conf);
	$result = $db->field(array("*"))->where("Id<>1")->select("sc_admin");
	return $result;
}

function power(){
	if(isset($_SESSION['a'])){
		if($_SESSION['a']['alevel']<3){
			session_destroy();
			echo "<script>alert('未授权登录');window.location.href='/index.php';</script>";exit;
		}
		$time = time() - $_SESSION['a']['atime'];
		if($time>1800){
			session_destroy();
			if(isset($_GET['act'])){
			echo json_encode(array(0,"登录超时，请重新登录"));exit;}
			else{
			echo "<script>alert('登录超时，请重新登录');window.location.href='login.php';</script>";exit;}
		}
		else{
			$_SESSION['a']['atime'] = time();
		}
	}
	else{
		if(isset($_GET['act'])){
		echo json_encode(array(0,"您还未登录"));exit;}
		else{
		echo "<script>window.location.href='login.php';</script>";exit;}
	}
}

?>